<template>
	<view class="ts-flex ts-column ts-flex-item">

		<!-- 弹出窗口 -->
		<ts-popup v-model="hidden">
			<view class="ts-grid-9">
				<view class="ts-grid-9-item" hover-class="ts-grid-9-item-hover" @tap="navigateTo('add')">
					<ts-icon type="plus" size="80" color="#d81e06"></ts-icon>
					<text class="ts-grid-9-text ts-padding">发布</text>
				</view>
				<view class="ts-grid-9-item" hover-class="ts-grid-9-item-hover" @tap="navigateTo('my')">
					<ts-icon type="person" size="80" color="#d81e06"></ts-icon>
					<text class="ts-grid-9-text ts-padding">我的</text>
				</view>
			</view>
			<button type="warn" style="width: 100%; background-color: #e31436; 	border-radius: 0upx; height: 100upx;" @tap="hide">关闭</button>
		</ts-popup>
		<!-- 弹出窗口 -->

		<!-- FAB -->
		<view class="ts-fab" @tap="showPopupWindow">
			<image src="../../../static/plus.png" />
			<text v-show="messageCount > 0" class="ts-badge ts-badge-danger">{{messageCount}}</text>
		</view>
		<!-- FAB -->

		<ts-banner image='https://via.placeholder.com/750x250' height="250"></ts-banner>



		<view class="ts-row" style="background-color: #FFF;">
			<navigator url="../app/store/index" hover-class="navigator-hover" class="ts-flex-item">
				<view class="ts-column ts-flex-item  ts-center ts-padding">
					<image style="width: 80upx; height: 80upx;" src="../../../static/plus.png"></image>
					<text>企业圈</text>
				</view>
			</navigator>

			<navigator url="../app/jingxuan/index" hover-class="navigator-hover" class="ts-flex-item">
				<view class="ts-column ts-flex-item  ts-center ts-padding">
					<image style="width: 80upx; height: 80upx;" src="../../../static/plus.png"></image>
					<text class="ts-h5">社团圈</text>
				</view>
			</navigator>

			<navigator url="../app/mooc/index" hover-class="navigator-hover" class="ts-flex-item">
				<view class="ts-column ts-flex-item  ts-center ts-padding">
					<image style="width: 80upx; height: 80upx;" src="../../../static/plus.png"></image>
					<text class="ts-h5">工作圈</text>
				</view>
			</navigator>

		</view>

		<view class="ts-gap"></view>
		<view class="ts-row" style="justify-content: space-between; align-items: center; padding: 10upx;">
			<view class="ts-row">我的圈子</view>
			<view class="ts-row ts-center">
				<view class="ts-h6">
					查看全部
				</view>
				<ts-icon type="arrowright" size="28" color="#bbb"></ts-icon>
			</view>
		</view>
		<view class="ts-list">
			<view class="ts-list-cell" hover-class="ts-list-cell-hover">
				<view class="ts-media-list">
					<image class="ts-media-list-logo" style="width: 80upx; height: 80upx;" src="../../../static/myHL.png"></image>
					<view class="ts-media-list-body">
						<view class="ts-media-list-text-top">篮球协会</view>
						<view class="ts-media-list-text-bottom ts-ellipsis">篮球协会篮球协会篮球协会篮球协会篮球协会篮球协会篮球协会</view>
					</view>
				</view>
			</view>
			<view class="ts-list-cell" hover-class="ts-list-cell-hover">
				<view class="ts-media-list">
					<image class="ts-media-list-logo" style="width: 80upx; height: 80upx;" src="../../../static/myHL.png"></image>
					<view class="ts-media-list-body">
						<view class="ts-media-list-text-top">篮球协会</view>
						<view class="ts-media-list-text-bottom ts-ellipsis">篮球协会篮球协会篮球协会篮球协会篮球协会篮球协会篮球协会</view>
					</view>
				</view>
			</view>
			<view class="ts-list-cell" hover-class="ts-list-cell-hover">
				<view class="ts-media-list">
					<image class="ts-media-list-logo" style="width: 80upx; height: 80upx;" src="../../../static/myHL.png"></image>
					<view class="ts-media-list-body">
						<view class="ts-media-list-text-top">篮球协会</view>
						<view class="ts-media-list-text-bottom ts-ellipsis">篮球协会篮球协会篮球协会篮球协会篮球协会篮球协会篮球协会</view>
					</view>
				</view>
			</view>
		</view>


		<view class="ts-gap"></view>
		
		<navigator url="add" hover-class="navigator-hover">
		<view class="ts-row" style="justify-content: space-between; align-items: center; padding: 10upx;">
			<view class="ts-row">校园活动</view>
			<view class="ts-row ts-center ts-padding-right">
				<ts-icon type="plusempty" size="28" color="#bbb"></ts-icon>
				<view class="ts-h6">
					发布活动
				</view>				
			</view>
		</view>
		</navigator>
		
		<view class="ts-row" style="background-color: #C0C0C0;">
		<ts-search-bar v-model="keywords" @search="doSearch"></ts-search-bar>
		</view>

		<ts-segmented-control :values="tabs" v-model="currentTab" style-type="text" active-color="#d81e06" @clickItem="clickItem"
		 borderColor="#d81e06"></ts-segmented-control>

		<view class="ts-list">
			<view class="ts-list-cell" v-for="(item,index) in list" hover-class="navigator-hover" :key="index" @tap="navigateToDetail(item.id)">
				<view class="ts-media-list">

					<image class="ts-media-list-logo" :src="item.cover_image"></image>
					<view class="ts-flex-item ts-column" style="justify-content: space-between;">
						<view class="ts-h5 ts-ellipsis-2">{{item.title}}</view>
						<view class="ts-h5">{{item.organizer}}</view>
						<view class="ts-h5">{{item.address}}</view>
						<view class="ts-h5">{{item.time}}</view>
						<view class="ts-row">
							<ts-badge :text="item.type" type="primary"></ts-badge>
							<ts-badge :text="item.status" type="danger" :padding-left="10"></ts-badge>
						</view>
					</view>
				</view>
			</view>
		</view>



		<view class="ts-flex ts-column">
			<ts-load-more :loadingType="loadingType"  @tap="getEventListMoreData"></ts-load-more>
		</view>

	</view>
</template>

<script>
	import tsSegmentedControl from '@/components/teaset/components/ts-segmented-control.vue';
	import tsBanner from '@/components/teaset/components/ts-banner.vue';
	import tsSearchBar from '@/components/teaset/components/ts-search-bar.vue';
	import tsPopup from '@/components/teaset/components/ts-popup.vue';

	// var dateUtils = require('common/util.js').dateUtils;

	export default {
		components: {
			tsBanner,
			tsSegmentedControl,
			tsSearchBar,
			tsPopup
		},
		data() {
			return {

				hidden: true,
				currentTab: 0,
				tabs: ['全部', '学校', '院系', '班级', '社团'],
				keywords: '',
				loadingType: 0, //上拉的状态：0-loading前；1-loading中；2-没有更多了
				page: 0,
				list: []
			}
		},
		onLoad() {
			this.doSearch();
		},
		onPullDownRefresh() {
			this.doSearch()
		},
		onReachBottom() {
			// console.log('onReachBottom');
			this.getEventListMoreData();
		},
		computed: {
			messageCount() {
				return this.list.length;
			}
		},
		methods: {
			showPopupWindow() {
				this.hidden = false;
			},
			hide() {
				this.hidden = true;
			},
			navigateTo(url) {
				this.hide();
				if (url) {
					uni.navigateTo({
						url: url,
					})
				}
			},
			navigateToDetail(id) {
				uni.navigateTo({
					url: `detail?id=${id}`
				});
			},
			callTelphone(tel) {
				uni.makePhoneCall({
					phoneNumber: tel
				});
			},
			clickItem() {
				this.doSearch()
			},
			doSearch() {
				//重置状态数据
				this.list = [];
				this.page = 0;
				this.loadingType = 0;
				this.getEventListMoreData();
			},
			getEventListMoreData() {

				//上拉的状态：0-loading前；1-loading中；2-没有更多了
				if (this.loadingType !== 0) {
					return;
				}
				this.loadingType = 1;
				this.page = this.page + 1;

				uni.request({
					url: this.$API_BASE + `/app/activity/getEventList`,
					method: 'GET',
					dataType: 'json',
					data: {
						keywords: this.keywords,
						page: this.page,
						type: this.tabs[this.currentTab],
					},
					success: (response) => {
						const res = response.data;

						if (res.errno===0) {
							const data = res.data;
							if (data && data.data) {
								this.list = this.list.concat(data.data);
							}

							if (data.totalPages === data.currentPage) {
							this.loadingType = 2; //2-没有更多了
						} else {
							this.loadingType = 0; //开启新一轮加载
						}

							uni.stopPullDownRefresh();

						} else {
							console.log(res.errmsg);
						}
					}
				});
			}
		},
	}
</script>

<style>
	.ts-media-list {
		padding: 20upx 20upx;
	}

	.ts-media-list-logo {
		padding: 0;
		width: 300upx;
		height: 230upx;
	}

	.ts-fab {
		position: fixed;
		display: block;
		z-index: 997;
		bottom: 50upx;
		right: 50upx;

		width: 100upx;
		height: 100upx;
		border-radius: 100upx;
		margin: 0 40upx;
		background-color: #e96900;
	}

	.ts-fab image {
		width: 60upx;
		height: 60upx;
		margin: 20upx;
	}

	.ts-fab text {
		position: fixed;
		bottom: 100upx;
		right: 80upx;
	}
</style>
